package com.dream.keigezhushou.Activity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dream.keigezhushou.Activity.acty.listen.golden.Golde30MyCollectedActivity;
import com.dream.keigezhushou.R;


import java.util.List;

/**
 * Created by mxmf-001 on 17/2/22.
 */
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {

    private View headView;
    private View footView;

    private static final int HEAD = 1;
    private static final int FOOT = 2;
    public static final int NORMAL = 3;

    private List<String> mDatas;
    private Context context;

    private Golde30MyCollectedActivity.MyItemClickListener mListener;


    public HomeAdapter(Context context, List<String> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }


    public void addHeadView(View headView) {
        this.headView = headView;
    }

    public void addFootView(View footView) {
        this.footView = footView;
    }

    public int getHeadViewCount() {
        return headView == null ? 0 : 1;
    }

    public int getFootViewCount() {
        return footView == null ? 0 : 1;
    }

    @Override
    public HomeAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        HomeAdapter.MyViewHolder holder = new HomeAdapter.MyViewHolder(LayoutInflater.from(
//                context).inflate(R.layout.collected_music_list_item, parent,
//                false));
        if (viewType == HEAD) {
            return new MyViewHolder(headView);
        }
        if (viewType == FOOT) return new MyViewHolder(footView);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recentplay_rcv, null);

        return new MyViewHolder(view);
    }

    public void setOnItemClickListener(Golde30MyCollectedActivity.MyItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < getHeadViewCount()) return HEAD;
        if (position >= mDatas.size() + getHeadViewCount()) return FOOT;


        return NORMAL;
    }

    @Override
    public void onBindViewHolder(HomeAdapter.MyViewHolder holder, final int position) {

        if (getItemViewType(position) == HEAD) return;
        if (getItemViewType(position) == FOOT) return;

        holder.tv.setText(mDatas.get(position - getHeadViewCount()));

        holder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mListener != null) {
                    mListener.onBtnClick(position);
                }
            }
        });


//        holder.itemLinear.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//
//                if(mListener!=null){
//
//                    mListener.onItemClick(position);
//
//                }
//            }
//        });



        headView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Toast.makeText(context,"头部",Toast.LENGTH_SHORT).show();
            }
        });

        footView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"脚步",Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size() + getHeadViewCount() + getFootViewCount();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView more;
        TextView tv;
        public LinearLayout itemLinear;
        TextView tvSinger;
        Button btn_delete;
        public MyViewHolder(final View view) {
            super(view);
            if (itemView == headView) return;
            if (itemView == footView) return;
            itemLinear = (LinearLayout) view.findViewById(R.id.item_music_list_recent);
            tv = (TextView) view.findViewById(R.id.collected_music_name);
            more = (ImageView) view.findViewById(R.id.item_more);
            btn_delete=(Button)view.findViewById(R.id.btn_from_delete);
            tvSinger= (TextView) view.findViewById(R.id.collected_music_singer);
        }


    }
    public void removeItem(int position){
        mDatas.remove(position);
        notifyDataSetChanged();

    }

}

